<template>
  <a-drawer width="35%" :label-col="4" :wrapper-col="14" :visible="open" @close="onClose">
    <a-divider orientation="left">
      <b>{{ formTitle }}</b>
    </a-divider>
    <a-form-model ref="form" :model="form" :rules="rules">
      <a-form-model-item label="选择卖家" prop="sellerUserId" >
        <a-select v-model="form.sellerUserId" @change="handleChange" placeholder="请选择卖家" :filterOption="filterOption" showSearch>
          <a-select-option v-for="item in superList" :key="item.id">
            {{ item.nickName }}({{ item.userPhone }})
          </a-select-option>
        </a-select>
      </a-form-model-item>
      <a-form-model-item label="买入价" prop="oldPrice" >
        <a-input v-model="form.oldPrice" placeholder="请输入买入价" />
      </a-form-model-item>
      <a-form-model-item label="卖出价" prop="price" >
        <a-input v-model="form.price" placeholder="请输入卖出价" />
      </a-form-model-item>
      <div class="bottom-control">
        <a-space>
          <a-button type="primary" :loading="submitLoading" @click="submitForm">
            保存
          </a-button>
          <a-button type="dashed" @click="cancel">
            取消
          </a-button>
        </a-space>
      </div>
    </a-form-model>
  </a-drawer>
</template>

<script>
import { getQdOrder, addQdOrder, updateQdOrder } from '@/api/system/qdOrder'
import {getListQdUser} from "@/api/system/qdUser";

export default {
  name: 'CreateForm',
  props: {
    statusOptions: {
      type: Array,
      required: true
    },
    sellerBalanceOptions: {
      type: Array,
      required: true
    },
    buyerBalanceOptions: {
      type: Array,
      required: true
    },
    sellerStatusOptions: {
      type: Array,
      required: true
    },
    buyerStatusOptions: {
      type: Array,
      required: true
    }
  },
  components: {
  },
  data () {
    return {
      superList: [],
      submitLoading: false,
      formTitle: '',
      // 表单参数
      form: {
        id: null,

        status: null,

        createBy: null,

        createTime: null,

        updateBy: null,

        updateTime: null,

        orderNo: null,

        sellerUserId: null,

        sellerUserName: null,

        sellerUserPhone: null,

        sellerBalance: null,

        buyerUserId: null,

        buyerUserName: null,

        buyerUserPhone: null,

        buyerBalance: null,

        oldPrice: null,

        price: null,

        newPrice: null,

        listingFee: null,

        sellerStatus: null,

        buyerStatus: null,

        goodImg: null,

        goodId: null,

        goodName: null

      },
      // 1增加,2修改
      formType: 1,
      open: false,
      rules: {
        sellerUserId: [
          { required: true, message: '请选择卖家', trigger: 'blur' }
        ],
        oldPrice: [
          { required: true, message: '请输入买入价', trigger: 'blur' }
        ],
        price: [
          { required: true, message: '请输入卖出价', trigger: 'blur' }
        ]
      }
    }
  },
  filters: {
  },
  created () {
  },
  computed: {
  },
  watch: {
  },
  mounted () {
    getListQdUser().then(res => {
      this.superList = res.data
      console.log(res.data)
    })
  },
  methods: {
    filterOption(inputValue, option) {
      const obj = this.superList.find(e => e.id === option.key)
      return (obj.nickName.indexOf(inputValue) > -1) || (obj.userPhone.indexOf(inputValue) > -1)
    },
    handleChange(val) {
      console.log(val)
    },
    onClose () {
      this.open = false
    },
    // 取消按钮
    cancel () {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset () {
      this.formType = 1
      this.form = {
        id: null,

        status: null,

        createBy: null,

        createTime: null,

        updateBy: null,

        updateTime: null,

        orderNo: null,

        sellerUserId: null,

        sellerUserName: null,

        sellerUserPhone: null,

        sellerBalance: null,

        buyerUserId: null,

        buyerUserName: null,

        buyerUserPhone: null,

        buyerBalance: null,

        oldPrice: null,

        price: null,

        newPrice: null,

        listingFee: null,

        sellerStatus: null,

        buyerStatus: null,

        goodImg: null,

        goodId: null,

        goodName: null

      }
    },
    /** 新增按钮操作 */
    handleAdd (row) {
      this.reset()
      this.formType = 1
      this.open = true
      this.formTitle = '添加'
    },
    /** 修改按钮操作 */
    handleUpdate (row, ids) {
      this.reset()
      this.formType = 2
      const id = row ? row.id : ids
      getQdOrder(id).then(response => {
        this.form = response.data
        this.open = true
        this.formTitle = '修改'
      })
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.submitLoading = true
          if (this.form.id !== undefined && this.form.id !== null) {
            updateQdOrder(this.form).then(response => {
              this.$message.success(
                '修改成功',
                3
              )
              this.open = false
              this.$emit('ok')
            }).finally(() => {
              this.submitLoading = false
            })
          } else {
            addQdOrder(this.form).then(response => {
              this.$message.success(
                '新增成功',
                3
              )
              this.open = false
              this.$emit('ok')
            }).finally(() => {
              this.submitLoading = false
            })
          }
        } else {
          return false
        }
      })
    }
  }
}
</script>
